using System;
using System.Text ;
using System.Data ;
using System.Collections ;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.MF.DataAccess;
using WP.MF.DataEntity;

namespace WP.MF.BusinessRule
{
	/// <summary>
	/// Summary description for MBranchBRL.
	/// </summary>
	public class MBranchBRL:RuleBase
	{
        private string strThisClassName = "MBranchBRL";

		public MBranchBRL()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		#region CommonValid
        //public bool CommonValid(DBAccess dBAccess,MBranchDEL mBranchDEL)
        //{
        //    bool bReturn=true;
        //    bool bTemp=true;
        //    DataRow DR=mBranchDEL.Tables[MBranchDEL.TABLENAME_MBRANCH].Rows[0];
        //    //Account 			
        //    if (DR[MBranchDEL.INVACCTNO].ToString().Trim() != "" && DR[MBranchDEL.INVPROFITNO ].ToString().Trim()=="")
        //    {
        //        bTemp=IsRecordExisted(dBAccess,"glAccount","AcctNo",DR[MBranchDEL.INVACCTNO].ToString());
        //    }
        //    else
        //    {
        //        if (DR[MBranchDEL.INVPROFITNO].ToString().Trim() != "" && DR[MBranchDEL.INVACCTNO ].ToString().Trim()=="")
        //        {
        //            bTemp=IsRecordExisted(dBAccess,"glAccount", "ProfitNo",DR[MBranchDEL.INVPROFITNO].ToString());
        //        }
        //        else
        //        {
        //            if (DR[MBranchDEL.INVPROFITNO].ToString().Trim() != "" && DR[MBranchDEL.INVACCTNO ].ToString().Trim()!="")
        //            {
        //                    bTemp=IsRecordExisted(dBAccess,"glAccount","AcctNo",DR[MBranchDEL.INVACCTNO].ToString(),"ProfitNo",DR[MBranchDEL.INVPROFITNO].ToString(),"BranchNo",DR[MBranchDEL.BRANCHNO].ToString());
        //            }
					
        //        }
        //    }
			
        //    if(!bTemp)
        //    {
        //        SysVar.ErrorMessage.Add(DataFunctions.GetMsg("AccountNotExist"));
        //        bReturn &= bTemp;
        //    }
			
        //    //POOrdNo
        //    if(DR[MBranchDEL.POORDNO].ToString()!="")
        //    {
        //        if (!BaseFunctions.IsNumeric(DR[MBranchDEL.POORDNO].ToString()))
        //        {
        //            SysVar.ErrorMessage.Add(DataFunctions.GetMsg(this.GetType().Name, "MSG_PONoIsnotNumericType"));
        //            bReturn &= bTemp;
        //        }
        //    }

        //    return bReturn;
        //}
		#endregion CommonValid

		#region Insert MBranch Valid
		public bool InsertValid(DBAccess dBAccess,MBranchDEL mBranchDEL)
		{
			bool bReturn = true;
            string strErrPrompt = string.Empty;
			SysVar.ErrorMessage.Clear();

            DataView dvwCheck = dBAccess.GetDataTable("select * from mnamedict where NeedCheck=1 and tablename='" + MBranchDEL.TABLENAME_MBRANCH + "'").DefaultView;
            foreach (DataRowView drw in dvwCheck)
            {
                if (!FieldCheck(dBAccess, mBranchDEL.Tables[MBranchDEL.TABLENAME_MBRANCH].Rows[0], drw["FieldName"].ToString(), ref strErrPrompt, FormStatus.AddNew))
                {
                    bReturn = false;
                    if (SysVar.ErrorMessage.Count == 0)
                    {
                        SysVar.ErrorMessage.Add(new ErrInfo(strErrPrompt, drw["FieldName"].ToString()));
                    }
                    break;
                }
            }
			return bReturn;
		}
		#endregion

		#region Update MBranch Valid
		public bool UpdateValid(DBAccess dBAccess,MBranchDEL mBranchDEL)
		{
			bool bReturn = true;
            string strErrPrompt = string.Empty;
            SysVar.ErrorMessage.Clear();

			if(IsExistedAndNotChanged(dBAccess,mBranchDEL.Tables[MBranchDEL.TABLENAME_MBRANCH]))
			{
                DataView dvwCheck = dBAccess.GetDataTable("select * from mnamedict where NeedCheck=1 and tablename='" + MBranchDEL.TABLENAME_MBRANCH + "'").DefaultView;
                foreach (DataRowView drw in dvwCheck)
                {
                    if (!FieldCheck(dBAccess, mBranchDEL.Tables[MBranchDEL.TABLENAME_MBRANCH].Rows[0], drw["FieldName"].ToString(), ref strErrPrompt, FormStatus.Update))
                    {
                        bReturn = false;
                        if (SysVar.ErrorMessage.Count == 0)
                        {
                            SysVar.ErrorMessage.Add(new ErrInfo(strErrPrompt, drw["FieldName"].ToString()));
                        }
                        break;
                    }
                }

			}
			else
			{
				bReturn=false;
			}
			return bReturn;
		}

		#endregion

		#region Delete MBranch Valid
		public bool DeleteValid(DBAccess dBAccess,string strRecId,byte[] btOldRecStamp)
		{
			SysVar.ErrorMessage.Clear();

			//the error was msgboxed, so just exit here
            if (!base.IsExistedAndNotChanged(dBAccess, "MBranch", strRecId, btOldRecStamp))
            {
                return false;
            }

			string strBranchNo=DataFunctions.GetFieldValue(dBAccess, "mBranch", "BranchNo", "recid=" + strRecId);
			if	(IsRecordExisted(dBAccess,"glAccount","BranchNo",BaseFunctions.SpecialCharDeal( strBranchNo )))
			{
                SysVar.ErrorMessage.Add(Msg.Select("Can not delete: this Branchno exists in glAccount!", DataFunctions.GetMsg(this.GetType().Name, "MSG_BranchNoExistsInGlAccount")));
				return false;
			}

            if (MsgBoxEx.Show(Msg.Select("Are you sure to delete this branch? ", DataFunctions.GetMsg(this.GetType().Name, "MSG_DeleteThisBranch")), SysVar.ConfirmTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
            {
                return false;
            }
			return true;
		}

		#endregion

        #region Field Check
        public bool FieldCheck(DBAccess dbcon, DataRow dr, string strFieldName, ref string strErrPrompt, FormStatus formStatus)
        {
            strErrPrompt = string.Empty;
            if (strFieldName == null) return true;

            bool blnExistsField = false;
            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                if (dr.Table.Columns[i].Caption.ToUpper().Trim() == strFieldName.Trim().ToUpper())
                {
                    blnExistsField = true;
                    break;
                }
            }
            if (!blnExistsField) return true;
            string strFieldValue = CvtFunc.ToString(dr[strFieldName.Trim()]);
            string strBranchNo = CvtFunc.ToString(dr[MBranchDEL.BRANCHNO]);
            string strInvAcctNo = CvtFunc.ToString(dr[MBranchDEL.INVACCTNO]);
            string strInvProfitNo = CvtFunc.ToString(dr[MBranchDEL.INVPROFITNO]);

            switch (strFieldName.ToUpper().Trim())
            {
                #region BranchNo
                case "BRANCHNO":
                    if (formStatus == FormStatus.AddNew)
                        return RuleBase.FieldValueCheckOne(dbcon, strFieldValue, MBranchDEL.TABLENAME_MBRANCH, strFieldName, ref  strErrPrompt);
                    else
                        return true;
                #endregion

                #region InvAcctNo, ProfitNo
                case "INVACCTNO":
                case "INVPROFITNO":
                    if (strInvAcctNo.Trim() != string.Empty && strInvProfitNo.Trim() != string.Empty)
                    {
                        if (!DataFunctions.CheckNoInActiveAccount(dbcon, strInvAcctNo, strInvProfitNo, strBranchNo, true, "", "", ref strErrPrompt))
                            return false;
                    }
                    return true;
                #endregion
                default:
                    return RuleBase.FieldValueCheckOne(dbcon, strFieldValue, MBranchDEL.TABLENAME_MBRANCH, strFieldName, ref strErrPrompt);
            }
        }
        #endregion
    }
}


